package com.ossean.projectmanager.syncmatchresult;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.ossean.projectmanager.lasttabledao.RelativeMemoDao;
import com.ossean.projectmanager.lasttabledao.RelativeMemoToOpenSourceProjectDao;
import com.ossean.projectmanager.model.RelativeMemo;
import com.ossean.projectmanager.model.RelativeMemoToOpenSourceProject;

@Component("syncMatchResult")
public class SyncMatchResult {
	@Resource
	private RelativeMemoDao lastMemoDao;

	@Resource
	private RelativeMemoToOpenSourceProjectDao matchResultDao;

	Logger logger = Logger.getLogger(this.getClass());

	/**
	 * 从帖子表中同步帖子信息到匹配结果表
	 * 
	 * @throws InterruptedException
	 */
	public void syncMatchResult() {
		while (true) {
//			int notSyncNum = matchResultDao.getNotSyncCount("relative_memo_to_open_source_projects_1");
//			if (notSyncNum < 1) {
//				try {
//					logger.info("sleeping 10s......");
//					Thread.sleep(10000);
//				} catch (InterruptedException e) {
//					logger.error(e);
//				}
//			}
			for (int i = 1; i <= 70; i++) {
				String matchResultTableName = "relative_memo_to_open_source_projects_" + String.valueOf(i);
				//logger.info("starting sync " + matchResultTableName + "......");
				//long startReadMT = System.currentTimeMillis();
				List<RelativeMemoToOpenSourceProject> rmtospList = matchResultDao.getMatchResultsByHassync(matchResultTableName, 50000);
				//long endReadMT = System.currentTimeMillis();
				//logger.info("get rmtospList： " + (endReadMT - startReadMT)/6000 + " s");
				if (rmtospList !=null && rmtospList.size() > 0) {
					for (RelativeMemoToOpenSourceProject rmtosp : rmtospList) {
						int curMemoId = rmtosp.getRelative_memo_id();
						RelativeMemo curMemo = lastMemoDao.getById(curMemoId);
						rmtosp.setCreated_time(curMemo.getCreated_time());
						rmtosp.setMemo_type(curMemo.getMemo_type());
						rmtosp.setReplies_num(curMemo.getReplies_num());
						rmtosp.setView_num_crawled(curMemo.getView_num());
						matchResultDao.syncMatchResult(matchResultTableName, rmtosp);
						logger.info("updated " + rmtosp.getId() + "......");
					}
				}
				//long endCircle = System.currentTimeMillis();
				//logger.info("endCircle : " + (endCircle - endReadMT)/6000 +" s");
			}
		}	
			
	}

}
